using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.DataAccess;

namespace IGlobalist.Cms.Bll
{
    public class ProductController : BaseController
    {
        public static IProduct ConvertCmsObject2Product(ICmsObject obj)
        {
            IProduct product = new Product();

            product.FromCmsObject(obj);
           
            return product;
        }


        private static IList<IProduct> ConvertListObject2Products(IList<ICmsObject> list)
        {
            IList<IProduct> products = new List<IProduct>();
            if (list != null)
                foreach (ICmsObject obj in list)
                {
                    if (obj.ClassTypeID == (short)CmsObjectType.Product)
                    {
                        products.Add(ConvertCmsObject2Product(obj));
                    }
                }

            return products;
        }

        public static ProductList ConvertListObject2ProductList(IList<ICmsObject> list)
        {
            ProductList products = new ProductList();
            if (list != null)
                foreach (ICmsObject product in list)
                {
                    if (product.ClassTypeID == (short)CmsObjectType.Product)
                    {
                        products.Add(ConvertCmsObject2Product(product));
                    }
                }
            return products;
        }




        public static IProduct GetProduct(long id)
        {
            return ConvertCmsObject2Product(_Ds.GetObject(id));
        }



        public static IList<IProduct> GetProductList(long classID)
        {
            return ConvertListObject2Products(_Ds.GetObjectListByClassID(classID));
        }




        public static ProductList GetProductListLelateToObjectID(long objectID, CmsObjectType objectType)
        {
            return ConvertListObject2ProductList(_Ds.GetObjectListRelateToObjectID(objectID, (short)objectType));
        }


        public static ProductList GetProductListAvailable()
        {


            return ConvertListObject2ProductList(_Ds.GetObjectListAllByClassTypeID((short)CmsObjectType.Product));
        }

        public static ProductList GetProductListBySearch(string search)
        {
            return ConvertListObject2ProductList(_Ds.GetObjectListAllBySearch(search));
        }

        public static long InsertProduct(IProduct product)
        {
            long id = _Ds.InsertObject((ICmsObject)product);
            if (product.PageID > 0)
            {
                _Ds.InsertObjectRelation(product.PageID, id, (short)CmsObjectType.Page);
            }

            if (product.CategoryID > 0)
            {
                _Ds.InsertObjectRelation(product.CategoryID, id, (short)CmsObjectType.ContentCategory);
            }


            


            return id;
        }

       

        public static void UpdateProduct(IProduct product)
        {
            IProduct productOld = GetProduct(product.ID);
            _Ds.UpdateObject((ICmsObject)product);


            if (productOld.PageID != product.PageID)
            {
                _Ds.DeleteObjectOneRelation(productOld.PageID, product.ID);
                if (product.PageID > 0)
                {
                    _Ds.InsertObjectRelation(product.PageID, product.ID, (short)CmsObjectType.Page);
                }
            }

            if (productOld.CategoryID != product.CategoryID)
            {
                _Ds.DeleteObjectOneRelation(productOld.CategoryID, product.ID);
                if (product.CategoryID > 0)
                {
                    _Ds.InsertObjectRelation(product.CategoryID, product.ID, (short)CmsObjectType.ContentCategory);
                }
            }

           

            _Ds.ClearCacheData((short)CmsObjectType.Page);

        }

        public static void DeleteProduct(long id)
        {
            _Ds.DeleteObject(id);
        }
    }
}
